home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / gfx / misc / gnuplot-src.lha / gnuplot-3.7.1src / gnuplot-3.7.1.lha / gnuplot-3.7.1 / docs / termdoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-11-03  |  6.0 KB  |  217 lines

  1. /*
  2.  * $Id: t}
  3.     re998/11/03 12:47:57 lhecking Exp $
  4.  *
  5.  */
  6.  
  7. /* GNUPLOT - termdoc.c */
  8.  
  9. /*[
  10.  * Copyright 1986 - 1993, 1998   Thomas Williams, Colin Kelley
  11.  *
  12.  * Permission to use, copy, and distribute this software and its
  13.  * documentation for any purpose with or without fee is hereby granted,
  14.  * provided that the above copyright notice appear in all copies and
  15.  * that both that copyright notice and this permission notice appear
  16.  * in supporting documentation.
  17.  *
  18.  * Permission to modify the software is granted, but not the right to
  19.  * distribute the complete modified source code.  Modifications are to
  20.  * be distributed as patches to the released version.  Permission to
  21.  * distribute binaries produced by compiling modified sources is granted,
  22.  * provided you
  23.  *   1. distribute the corresponding source modifications from the
  24.  *    released version in the form of a patch file along with the binaries,
  25.  *   2. add special version identification to distinguish your version
  26.  *    in addition to the base release version number,
  27.  *   3. provide your name and address as the primary contact for the
  28.  *    support of your modified version, and
  29.  *   4. retain our contact information in regard to use of the base
  30.  *    software.
  31.  * Permission to distribute the released version of the source code along
  32.  * with corresponding source modifications in the form of a patch file is
  33.  * granted with same provisions 2 through 4 for binary distributions.
  34.  *
  35.  * This software is provided "as is" without express or implied warranty
  36.  * to the extent permitted by applicable law.
  37. ]*/
  38.  
  39. /*
  40.  * AUTHORS
  41.  *
  42.  *  David Denholm - 1996
  43.  */
  44.  
  45. /* this file provides a replacement for fgets() which inserts all the
  46.  * help from the terminal drivers line by line at the < in the
  47.  * gnuplot.doc file. This way, doc2* dont need to know what's going
  48.  * on, and think it's all coming from one place.
  49.  *
  50.  * Can be compiled as a standalone program to generate the raw
  51.  * .doc test, when compiled with -DTEST_TERMDOC
  52.  *
  53.  * Strips comment lines {so none of doc2* need to bother} 
  54.  * but magic comments beginning  C#  are used as markers
  55.  * for line number recording (as c compilers)
  56.  * We set BEGIN_HELP macro to "C#<driver>" as a special marker.
  57.  *
  58.  * Hmm - this is turning more and more into a preprocessor !
  59.  * gnuplot.doc now has multiple top-level entries, but
  60.  * some help systems (eg VMS) cannot tolerate this.
  61.  * As a complete bodge, conditional on boolean single_top_level == TRUE,
  62.  * we accept only the first 1, and map any subsequent 1's to 2's
  63.  * At present, this leaves a bogus, empty section called
  64.  * commands, but that's a small price to pay to get it
  65.  * working properly
  66.  */
  67.  
  68. #ifdef HAVE_CONFIG_H
  69. # include "config.h"
  70. #endif
  71.  
  72. #define DOCS_TERMDOC_MAIN
  73.  
  74. #include "ansichek.h"
  75. #include "stdfn.h"
  76. #include "doc2x.h"
  77.  
  78. /* because we hide details of including terminal drivers,
  79.  * we provide line numbers and file names
  80.  */
  81.  
  82. int termdoc_lineno;
  83. char termdoc_filename[80];
  84.  
  85. boolean single_top_level;
  86.  
  87. char *get_line(buffer, max, fp)
  88. char *buffer;
  89. int max;
  90. FILE *fp;
  91. {
  92.     static int line = -1;    /* not going yet */
  93.     static int level = 0;    /* terminals are at level 1 - we add this */
  94.     static int save_lineno;    /* for saving lineno */
  95.     static int seen_a_one = 0;
  96.  
  97.     if (line == -1) {
  98.  
  99.     /* we are reading from file */
  100.  
  101.     {
  102.       read_another_line:    /* come here if a comment is read */
  103.  
  104.         if (!fgets(buffer, max, fp))
  105.         return NULL;    /* EOF */
  106.         ++termdoc_lineno;
  107.         if (buffer[0] == 'C') {
  108.         if (buffer[1] == '#') {
  109.             /* should not happen in gnuplot.doc, but... */
  110.             safe_strncpy(termdoc_filename, buffer + 2, sizeof(termdoc_filename));
  111.             termdoc_filename[strlen(termdoc_filename) - 1] = NUL;
  112.             termdoc_lineno = 0;
  113.         }
  114.         goto read_another_line;        /* skip comments */
  115.         }
  116.     }
  117.  
  118.     if (single_top_level == TRUE) {
  119.         if (buffer[0] == '1') {
  120.         if (seen_a_one) {
  121.             buffer[0] = '2';
  122.         }
  123.         seen_a_one = 1;
  124.         }
  125.     }
  126.     if (buffer[0] != '<')
  127.         return buffer;    /* the simple case */
  128.  
  129.     /* prepare to return text from the terminal drivers */
  130.     save_lineno = termdoc_lineno;
  131.     termdoc_lineno = -1;    /* dont count the C# */
  132.     level = buffer[1] - '1';
  133.     line = 0;
  134.     }
  135.     /* we're sending lines from terminal help */
  136.  
  137.     /* process and skip comments. Note that the last line
  138.      * will invariably be a comment !
  139.      */
  140.  
  141.     while (termtext[line][0] == 'C') {
  142.     if (termtext[line][1] == '#') {
  143.         safe_strncpy(termdoc_filename, termtext[line] + 2, sizeof(termdoc_filename));
  144.         termdoc_lineno = 0;
  145.     }
  146.     ++termdoc_lineno;
  147.  
  148.     if (!termtext[++line]) {
  149.         /* end of text : need to return a line from
  150.          * the file. Recursive call is best way out
  151.          */
  152.         termdoc_lineno = save_lineno;
  153.         /* we've done the last line, so get next line from file */
  154.         line = -1;
  155.         return get_line(buffer, max, fp);
  156.     }
  157.     }
  158.  
  159.  
  160.     /* termtext[line] is the next line of text.
  161.      * more efficient to return pointer, but we need to modify it
  162.      */
  163.  
  164.     ++termdoc_lineno;
  165.     safe_strncpy(buffer, termtext[line], max);
  166.     /* dodgy; can overrun buffer; lh */
  167.     /* strncat(buffer, "\n", max); */
  168.     if (strlen(buffer) == (max - 1))
  169.         buffer[max-2] = '\n';
  170.     else
  171.         strcat(buffer, "\n");
  172.         
  173.     if (isdigit((int)buffer[0]))
  174.     buffer[0] += level;
  175.  
  176.     if (!termtext[++line]) {
  177.     /* end of terminal help : return to input file next time
  178.      * last (pseudo-)line in each terminal should be a comment,
  179.      * so we shouldn't get here, but...
  180.      */
  181.     termdoc_lineno = save_lineno;
  182.     /* we've done the last line, so get next line from file */
  183.     line = -1;
  184.     }
  185.     return buffer;
  186. }
  187.  
  188.  
  189. /* Safe, '\0'-terminated version of strncpy()
  190.  * safe_strncpy(dest, src, n), where n = sizeof(dest)
  191.  * This is basically the old fit.c(copy_max) function
  192.  */
  193.  
  194. char *safe_strncpy(d, s, n)
  195. char *d, *s;
  196. size_t n;
  197. {
  198.     char *ret;
  199.  
  200.     ret = strncpy(d, s, n);
  201.     if (strlen(s) >= n)
  202.         d[n-1] = NUL;
  203.  
  204.     return ret;
  205. }
  206.  
  207.  
  208. #ifdef TEST_TERMDOC
  209. int main()
  210. {
  211.     char line[256];
  212.     while (get_line(line, sizeof(line), stdin))
  213.     printf("%s:%d:%s", termdoc_filename, termdoc_lineno, line);
  214.     return 0;
  215. }
  216. #endif
  217.